package com.ansion.commonchat

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import com.ansion.basedb.dao.LoginedUserMgr
import com.ansion.basedb.dao.model.CurrentUser
import com.ansion.commonchat.db.CocoDBFactory
import com.ansion.commonchat.ui.theme.CommonChatTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        CocoDBFactory.getInstance().getMainDB()?.preload()
        setContent {
            CommonChatTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        name = "Contact",
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    val user = CurrentUser().apply {
        this.loginId = "8618519291063"
        this.loginToken = "1234560"
        this.loginAccount = "8618519291063"
        this.nickName = "ansion"
        this.userId = 18519291063
        this.ucid = "1234560"
        this.country = "86"
        this.countryPhone = "18519291063"
    }
    LoginedUserMgr.setCurrentLoginedUser(user)
    val context = LocalContext.current // 获取当前上下文
    Text(
        text = "Hello $name!",
        modifier = modifier.clickable {
            Toast.makeText(context, "Hello $name!", Toast.LENGTH_SHORT).show()
            context.startActivity(Intent(context, SecondActivity::class.java))
        }
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    CommonChatTheme {
        Greeting("Android")
    }
}